package com.jlsoft.code;

import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 自定义类型处理器
 */
public class UseFlagEnumTypeHander implements TypeHandler<UseFlag> {
    /**
     * 定义当前的数据如何保存到DB中
     * @param preparedStatement
     * @param i
     * @param useFlag
     * @param jdbcType
     * @throws SQLException
     */
    @Override
    public void setParameter(PreparedStatement preparedStatement, int i, UseFlag useFlag, JdbcType jdbcType) throws SQLException {
        preparedStatement.setString(i, useFlag.getCode().toString());
    }

    /**
     * 从数据中拿到状枚举类型态码
     * @param resultSet
     * @param s
     * @return
     * @throws SQLException
     */
    @Override
    public UseFlag getResult(ResultSet resultSet, String s) throws SQLException {
        int code = resultSet.getInt(s);
        return UseFlag.getUseFlag(code);
    }

    @Override
    public UseFlag getResult(ResultSet resultSet, int i) throws SQLException {
        int code = resultSet.getInt(i);
        return UseFlag.getUseFlag(code);
    }

    @Override
    public UseFlag getResult(CallableStatement callableStatement, int i) throws SQLException {
        int code = callableStatement.getInt(i);
        return UseFlag.getUseFlag(code);
    }
}
